package com.ruoyi.develop.goodsOrderInner.service.impl;

import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsOrderInner.mapper.GoodsOrderInnerMapper;
import com.ruoyi.develop.goodsOrderInner.domain.GoodsOrderInner;
import com.ruoyi.develop.goodsOrderInner.service.IGoodsOrderInnerService;

/**
 * 厂内编码Service业务层处理
 * @author lmm
 * @date 2025-08-02
 */
@Service
public class GoodsOrderInnerServiceImpl implements IGoodsOrderInnerService {
    @Resource
    private GoodsOrderInnerMapper goodsOrderInnerMapper;

    /**
     * 查询厂内编码
     * @param id 厂内编码主键
     * @return 厂内编码
     */
    @Override
    public GoodsOrderInner selectById(String id) {
        return goodsOrderInnerMapper.selectById(id);
    }

    /**
     * 查询厂内编码列表
     * @param entity 厂内编码
     * @return 厂内编码
     */
    @Override
    public List<GoodsOrderInner> selectList(GoodsOrderInner entity) {
        return goodsOrderInnerMapper.selectList(entity);
    }

    /**
     * 新增厂内编码
     * @param entity 厂内编码
     * @return 结果
     */
    @Override
    public int insert(GoodsOrderInner entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
                entity.setCreateBy(SecurityUtils.getUsername());
                entity.setCreateTime(DateUtils.getNowDate());
            return goodsOrderInnerMapper.insert(entity);
    }

    /**
     * 修改厂内编码
     * @param entity 厂内编码
     * @return 结果
     */
    @Override
    public int update(GoodsOrderInner entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return goodsOrderInnerMapper.update(entity);
    }

    /**
     * 批量删除厂内编码
     * @param ids 需要删除的厂内编码主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsOrderInnerMapper.deleteByIds(ids);
    }

    /**
     * 删除厂内编码信息
     * @param id 厂内编码主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsOrderInnerMapper.deleteById(id);
    }

    @Override
    public String generateInnerCode(String goodsCode) {
        String codeMaxString = goodsOrderInnerMapper.selectMaxCode(goodsCode);
        if(StringUtils.isNotBlank(codeMaxString)){
            // 最大编号+1
            // 如果codeMaxString开头含0 编码生成后需要加0 装换Long时候不丢失0
            if(codeMaxString.startsWith("0")){
                codeMaxString = String.format("%0"+codeMaxString.length()+"d",Long.parseLong(codeMaxString)+1);
            }
        }else{
            codeMaxString = goodsCode+"001";
        }
        return codeMaxString;
    }


}
